﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class ITGuy : MonoBehaviour
{
    [SerializeField]
    private float minProblemGenerateInterval, maxProblemGenerateInterval;
    [SerializeField]
    private Problem[] problemScripts;

    private int tired, thirsty, hungry, pain, pressure;
    private List<Problem> problems = new List<Problem>();
    private float tiredTimer, thirstyTimer, hungryTimer, painTimer, pressureTimer;
    private float problemGenerateTimer, currentInterval;

    private float coroutineTime = 0.5f;
    private ITAnimationController animCon;

    public List<Problem> GetProblems()
    {
        return problems;
    }

    public void OnProblemSolved(ITGuyProblems problemType)
    {
        AudioManager.Instance.PlaySound("ProblemSolved");
        var problem = problems.Find(x => x.GetProblemType() == problemType);
        problems.Remove(problem);
        Destroy(problem.gameObject);
    }

    public void OnLose()
    {
        for (int i = 0; i < problems.Count; i++)
        {
            Destroy(problems[i].gameObject);
        }
    }

    private void Start()
    {
        animCon = GetComponent<ITAnimationController>();

        if (animCon != null)
        {
            StartCoroutine(HandleTellAnimController());
        }
    }

    private void Update()
    {
        if (GameFlowManager.Instance.CheckIfGameIsFinished())
            return;
        problemGenerateTimer += Time.deltaTime;
        if (problemGenerateTimer < currentInterval || problems.Count >= Enum.GetNames(typeof(ITGuyProblems)).Length)
            return;

        //Time to generate a new problem
        problemGenerateTimer = 0;
        currentInterval = UnityEngine.Random.Range(minProblemGenerateInterval, maxProblemGenerateInterval);

        var problemType = (ITGuyProblems) UnityEngine.Random.Range(0, Enum.GetNames(typeof(ITGuyProblems)).Length);
        //Design problem type
        while (problems.Exists(p => p.GetProblemType() == problemType))
        {
            problemType = (ITGuyProblems) UnityEngine.Random.Range(0, Enum.GetNames(typeof(ITGuyProblems)).Length);
        }

        GenerateProblem(problemType);
    }

    public void GenerateProblem(ITGuyProblems problem)
    {
        AudioManager.Instance.PlaySound("ProblemPop");
        var generatedProblem = Instantiate(problemScripts.First(p => p.GetProblemType() == problem));
        problems.Add(generatedProblem);
    }

    private IEnumerator HandleTellAnimController()
    {
        while (true)
        {
            yield return new WaitForSeconds(coroutineTime);
            
            if(animCon == null)
            {
                break;
            }

            if(problems.Count == 0)
            {
                animCon.SetDyingState(false);
            }
            else
            {
                animCon.SetDyingState(true);
            }
        }
    }
}